from rest_framework import serializers

from models.models import WaterDropletsTask, WaterDropletsRecord


class WaterDropletsTaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = WaterDropletsTask
        fields = "__all__"


class WaterDropletsRecordSerializer(serializers.ModelSerializer):
    op_type = serializers.SerializerMethodField()
    droplet_number = serializers.SerializerMethodField()
    user = serializers.SerializerMethodField()

    def get_op_type(self, obj):
        if obj.op_type == 1:
            return "获取"
        else:
            return "消耗"

    def get_droplet_number(self, obj):
        if obj.op_type == 1:
            return f"+{obj.droplet_number}"
        else:
            return f"-{obj.droplet_number}"

    def get_user(self, obj):
        return f"{obj.user.nickname}(ID:{obj.user.openid})"

    class Meta:
        model = WaterDropletsRecord
        fields = "__all__"
